package com.java.basis.collection;

import java.util.*;

/**
 * 测试Collections工具类的使用
 * @author wangning
 * @create 2021-04-24 14:41
 */
public class TestCollections01 {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<>();
		list.add(20);
		list.add(2);
		list.add(5);
		list.add(25);
		//排序
		System.out.println(list.toString());
		Collections.sort(list);
		System.out.println(list.toString());

		//二分查找
		int i = Collections.binarySearch(list, 25);
		System.out.println("i = " + i);
		int i1 = Collections.binarySearch(list, 24);
		System.out.println("i1 = " + i1);

		//copy 复制
		List<Integer> list1 = new ArrayList<>();
		for (int j = 0; j < list.size(); j++) {
			list1.add(0);
		}
		Collections.copy(list1, list);
		System.out.println("list1 = " + list1.toString());

		//reverse 反转
		Collections.reverse(list);
		System.out.println(list);

		//打乱shuffle
		Collections.shuffle(list);
		System.out.println("打乱："+ list);

		//list转数组
		Integer[] array = list.toArray(new Integer[5]);
		System.out.println("array.toString() = " + array.toString());
		//数组转list
		String[] names = {"张三", "李四", "王五"};
		List<String> list2 = Arrays.asList(names);
		//把数组改成集合，这是个受限集合，不允许添加删除元素
		System.out.println("list2 = " + list2);

//		int[] nums = {100, 200, 300, 400};//在使用基本类型转list时，会变成一个，如果想要转换可以使用Integer
		Integer[] nums = {100, 200, 300, 400};
		List<Integer> ints = Arrays.asList(nums);
		System.out.println("ints = " + ints);


	}
}
